package com.youtui.manager.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.youtui.common.constant.ResultMessage;
import com.youtui.common.util.ResultUtil;
import com.youtui.manager.model.Account;
import com.youtui.manager.model.AccountInfo;
import com.youtui.manager.model.PredictIncome;
import com.youtui.manager.service.IAccountInfoService;
import com.youtui.manager.service.IAccountService;
import com.youtui.manager.service.IPredictIncomeService;

@Controller
@RequestMapping("/account")
public class AccountController extends BaseController {

	@Autowired
	IAccountService accountService;

	@Autowired
	IAccountInfoService accountInfoService;

	@Autowired
	IPredictIncomeService predictIncomeService;

	@RequestMapping("/load.do")
	@ResponseBody
	public ResultMessage loadAccount(Long devId) {
		if (devId == null) {
			return ResultUtil.result(ResultUtil.code_fail, "请求参数错误");
		}

		Account account = accountService.findAccountByDevId(devId);

		if (account != null) {
			return ResultUtil.result(ResultUtil.code_success, "开发者账户信息", account);
		}
		return ResultUtil.result(ResultUtil.code_fail, "开发者账户信息不存在");
	}

	@RequestMapping("/loadAccountMoney.do")
	@ResponseBody
	public ResultMessage loadAccountMoney(Long devId) {
		if (devId == null) {
			return ResultUtil.result(ResultUtil.code_fail, "请求参数错误");
		}
		Account account = accountService.findAccountByDevId(devId);

		BigDecimal yester_income = new BigDecimal(0);
		PredictIncome predictIncome = predictIncomeService.findYesterPredictIncome(devId);
		if (predictIncome != null) {
			yester_income = predictIncome.getPredict();
		}

		BigDecimal month_income = predictIncomeService.findMonthPredictIncome(devId);
		Map<String, Object> map = new HashMap<String, Object>();

		if (account != null) {
			map.put("yester_income", yester_income);
			map.put("month_income", month_income);
			map.put("avail_income", account.getAvailWallet());
			map.put("total_income", account.getTotalWallet());
			return ResultUtil.result(ResultUtil.code_success, "开发者账户信息", map);
		}

		return ResultUtil.result(ResultUtil.code_fail, "开发者账户信息不存在");
	}

	/**
	 * 提现接口
	 * 
	 * @param devId
	 * @param applyMoney
	 * @return
	 */
	@RequestMapping("/withdrawal.do")
	@ResponseBody
	public ResultMessage withdrawal(Long devId, BigDecimal applyMoney) {

		if (devId == null || applyMoney == null || applyMoney.doubleValue() <= 0) {
			return ResultUtil.result(ResultUtil.code_fail, "请求参数错误");
		}
		AccountInfo accountInfo = accountInfoService.findAccountInfoByDevId(devId);
		if (accountInfo == null || StringUtils.isBlank(accountInfo.getBankNo())
				|| StringUtils.isBlank(accountInfo.getBankNo()) || StringUtils.isBlank(accountInfo.getBankName())) {
			return ResultUtil.result(ResultUtil.code_fail, "请先完善账户信息");
		}

		return accountService.updateWithdrawal(devId, applyMoney);
	}

}
